package com.code.config;

import org.apache.commons.lang3.StringUtils;

/**
 * type：数据库类型
 * driver：数据库驱动
 * tableSql：表格信息
 * columnSql：表格信息(详情)
 */
public enum DataSourceSql {
    mysql("mysql", "com.mysql.cj.jdbc.Driver",
            "" +
                    "SELECT " +
                    "    table_schema,table_name,table_comment " +
                    "FROM information_schema.tables WHERE table_schema='%s'",
            "" +
                    "SELECT " +
                    "    ordinal_position,table_schema,table_name,column_name, is_nullable,column_type,column_key," +
                    "    data_type,character_maximum_length,numeric_precision,numeric_scale,extra,column_comment " +
                    "FROM information_schema.columns " +
                    "WHERE table_schema='%s' and table_name ='%s' order by ordinal_position"),
    dm("dm", "dm.jdbc.driver.DmDriver",
            "" +
                    "SELECT " +
                    "   OWNER table_schema,TABLE_NAME table_name,COMMENTS AS table_comment " +
                    "FROM ALL_TAB_COMMENTS " +
                    "WHERE OWNER = '%s' AND TABLE_NAME IS NOT NULL AND TABLE_NAME != '##HISTOGRAMS_TABLE';",
            "" +
                    "SELECT " +
                    "    DTC.COLUMN_ID AS ordinal_position,DTC.OWNER AS table_schema,DTC.TABLE_NAME AS table_name, " +
                    "    DTC.COLUMN_NAME AS column_name,DTC.NULLABLE AS is_nullable,DTC.DATA_TYPE AS data_type," +
                    "    DTC.DATA_LENGTH AS character_maximum_length,DCC.COMMENTS AS column_comment," +
                    "    DTC.DATA_TYPE || '(' || TO_CHAR( DTC.DATA_LENGTH)|| ')' AS column_type," +
                    "    DCS.COLUMN_NAME extra " +
                    "FROM DBA_TAB_COLUMNS DTC " +
                    "LEFT JOIN DBA_COL_COMMENTS DCC ON (DTC.OWNER = DCC.OWNER AND DTC.TABLE_NAME = DCC.TABLE_NAME AND DTC.COLUMN_NAME = DCC.COLUMN_NAME) " +
                    "LEFT JOIN  DBA_CONS_COLUMNS DCS on (DTC.OWNER = DCS.OWNER AND DTC.TABLE_NAME = DCS.TABLE_NAME)" +
                    "WHERE DTC.OWNER = '%s'  AND  DTC.TABLE_NAME = '%s' ORDER BY DTC.COLUMN_ID;"),
    oracle("oracle", "", "", ""),
    sqlserver("sqlserver", "", "", ""),
    h2("h2", "", "", ""),
    hsql("hsql", "", "", ""),
    db2("db2", "", "", ""),
    postgresql("postgresql", "", "", ""),
    derby("derby", "", "", ""),
    ;

    private String type;
    private String driver;
    private String tableSql;
    private String columnSql;

    public static String getDriver(String type) {
        if (StringUtils.isEmpty(type)) {
            return null;
        }
        for (DataSourceSql valueInfo : DataSourceSql.values()) {
            if (type.equals(valueInfo.getType())) {
                return valueInfo.getDriver();
            }
        }
        return null;
    }

    public static String getTableSql(String type) {
        if (StringUtils.isEmpty(type)) {
            return null;
        }
        for (DataSourceSql valueInfo : DataSourceSql.values()) {
            if (type.equals(valueInfo.getType())) {
                return valueInfo.getTableSql();
            }
        }
        return null;
    }

    public static String getColumnSqlSql(String type) {
        if (StringUtils.isEmpty(type)) {
            return null;
        }
        for (DataSourceSql valueInfo : DataSourceSql.values()) {
            if (type.equals(valueInfo.getType())) {
                return valueInfo.getColumnSql();
            }
        }
        return null;
    }

    DataSourceSql(String type, String driver, String tableSql, String columnSql) {
        this.type = type;
        this.driver = driver;
        this.tableSql = tableSql;
        this.columnSql = columnSql;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getTableSql() {
        return tableSql;
    }

    public void setTableSql(String tableSql) {
        this.tableSql = tableSql;
    }

    public String getColumnSql() {
        return columnSql;
    }

    public void setColumnSql(String columnSql) {
        this.columnSql = columnSql;
    }
}
